﻿
@{
    ViewBag.Title = "设置权限";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model IList<Mate.Dto.System.MenuModel>
@section styles{

    <link href="~/bootstrap/plugins/treegrid/jquery.treegrid.css" rel="stylesheet" />
}

@section scripts{

    <script src="~/bootstrap/plugins/treegrid/jquery.treegrid.js"></script>
    <script src="~/bootstrap/plugins/treegrid/jquery.treegrid.bootstrap3.js"></script>

    <script type="text/javascript">
        $(function () {
            $('.tree').treegrid();

            $("#aSubmit").click(function () {
                var menuids = [];
                $("input[type='checkbox']:checked", $('.tree')).each(function () {
                    menuids.push($(this).val());
                });
                if (menuids.length == 0) {
                    layer.msg("请选择角色权限！", {
                        icon: 2,
                        time: 1500
                    });
                } else {
                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("AuthorizationManager")',
                        data: { roleid: "@(ViewBag.RoleId)", menuids: menuids.toString() },
                        success: function (result) {
                            if (result.resultCode == 1000) {
                                layer.msg(result.message, {
                                    icon: 1,
                                    time: 1500
                                }, function () {
                                    location.href = "@Url.Action("Index")";
                                });
                            } else {
                                layer.msg(result.message, {
                                    icon: 2,
                                    time: 1500
                                });
                            }
                        }
                    });
                }
            });

            $("input[type='checkbox']", $('.tree')).change(function () {
                var id = $(this).attr("id");
                var checked = $(this).prop("checked");
                $("input[type='checkbox']", $('.tree')).each(function () {
                    //console.log($(this).attr("id").indexOf(id))
                    //console.log($(this).attr("id"))
                    if ($(this).attr("id").indexOf(id) == 0) {
                        $(this).prop('checked', checked);

                    }
                });
                $.uniform.update();
            });
        });
    </script>
}

    <ul class="page-breadcrumb breadcrumb">
        <li>
            <a href="@Url.Action("Index","Roles")">
                角色管理
            </a>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            设置权限【@ViewBag.RoleName】
        </li>
    </ul>

<div class="row">

    <div class="col-md-12">
            <div class="portlet-body">
                <div class="row">
                    <div class="form-group">
                        <div class="col-md-8">
                            <div class="btn-group btn-group-solid">
                                <a href="javascript:void(0);" id="aSubmit" class="btn green" role="button" title="提交"><i class="fa fa-check"></i> 提交</a>
                                <a class="btn default" role="button" href="@Url.Action("Index")"> 返回 <i class="fa fa-reply"></i></a>
                            </div>
                        </div>
                    </div>
                </div><br />
                <table class="tree table table-hover table-bordered table-condensed">
                    <tr>
                        <th>菜单标题</th>
                        <th>功能</th>
                    </tr>
                    @if (Model != null)
                    {
                        foreach (var menu1 in Model)
                        {
                            <tr class="treegrid-@(menu1.id)">
                                <td><label><input type="checkbox" id="rm_@(menu1.id)" name="rm@(menu1.id)" value="@(menu1.id)" @(menu1.IsCurrentPermission ? "checked='checked'" : "") /> @(menu1.menu_name)</label></td>
                                <td>
                                    @if (menu1.children.Any(x => x.menu_type == 2))
                                    {
                                        foreach (var menu11 in menu1.children.Where(x => x.menu_type == 2))
                                        {
                                            <label><input type="checkbox" id="rm_@(menu1.id)_@(menu11.id)" name="rm@(menu11.id)" value="@(menu11.id)" @(menu11.IsCurrentPermission ? "checked='checked'" : "") /> @(menu11.menu_name)</label>
                                        }
                                    }
                                </td>
                            </tr>
                            if (menu1.children.Any(x => x.menu_type == 1))
                            {
                                foreach (var menu2 in menu1.children.Where(x => x.menu_type == 1))
                                {
                                    <tr class="treegrid-@(menu2.id) treegrid-parent-@(menu1.id)">
                                        <td><label><input type="checkbox" id="rm_@(menu1.id)_@(menu2.id)" name="rm@(menu2.id)" value="@(menu2.id)" @(menu2.IsCurrentPermission ? "checked='checked'" : "") /> @(menu2.menu_name)</label></td>
                                        <td>
                                            @if (menu2.children.Any(x => x.menu_type == 2))
                                            {
                                                foreach (var menu22 in menu2.children.Where(x => x.menu_type == 2))
                                                {
                                                    <label><input type="checkbox" id="rm_@(menu1.id)_@(menu2.id)_@(menu22.id)" name="rm@(menu22.id)" value="@(menu22.id)" @(menu22.IsCurrentPermission ? "checked='checked'" : "") /> @(menu22.menu_name)</label>
                                                }
                                            }
                                        </td>
                                    </tr>
                                }
                            }
                        }

                    }
                </table>

            </div>
        </div>
    </div>
